package cn.hyp.bantang.module.finance.controller.admin.basexj;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.hyp.bantang.framework.common.pojo.PageParam;
import cn.hyp.bantang.framework.common.pojo.PageResult;
import cn.hyp.bantang.framework.common.pojo.CommonResult;
import cn.hyp.bantang.framework.common.util.object.BeanUtils;
import static cn.hyp.bantang.framework.common.pojo.CommonResult.success;

import cn.hyp.bantang.framework.excel.core.util.ExcelUtils;

import cn.hyp.bantang.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hyp.bantang.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hyp.bantang.module.finance.controller.admin.basexj.vo.*;
import cn.hyp.bantang.module.finance.dal.dataobject.basexj.BaseXjDO;
import cn.hyp.bantang.module.finance.service.basexj.BaseXjService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 个股报告期现金")
@RestController
@RequestMapping("/finance/base-xj")
@Validated
public class BaseXjController {

    @Resource
    private BaseXjService baseXjService;

    @PostMapping("/create")
    @Operation(summary = "创建个股报告期现金")
    @PreAuthorize("@ss.hasPermission('finance:base-xj:create')")
    public CommonResult<Long> createBaseXj(@Valid @RequestBody BaseXjSaveReqVO createReqVO) {
        return success(baseXjService.createBaseXj(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新个股报告期现金")
    @PreAuthorize("@ss.hasPermission('finance:base-xj:update')")
    public CommonResult<Boolean> updateBaseXj(@Valid @RequestBody BaseXjSaveReqVO updateReqVO) {
        baseXjService.updateBaseXj(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除个股报告期现金")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('finance:base-xj:delete')")
    public CommonResult<Boolean> deleteBaseXj(@RequestParam("id") Long id) {
        baseXjService.deleteBaseXj(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得个股报告期现金")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('finance:base-xj:query')")
    public CommonResult<BaseXjRespVO> getBaseXj(@RequestParam("id") Long id) {
        BaseXjDO baseXj = baseXjService.getBaseXj(id);
        return success(BeanUtils.toBean(baseXj, BaseXjRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得个股报告期现金分页")
    @PreAuthorize("@ss.hasPermission('finance:base-xj:query')")
    public CommonResult<PageResult<BaseXjRespVO>> getBaseXjPage(@Valid BaseXjPageReqVO pageReqVO) {
        PageResult<BaseXjDO> pageResult = baseXjService.getBaseXjPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseXjRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出个股报告期现金 Excel")
    @PreAuthorize("@ss.hasPermission('finance:base-xj:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseXjExcel(@Valid BaseXjPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseXjDO> list = baseXjService.getBaseXjPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "个股报告期现金.xls", "数据", BaseXjRespVO.class,
                        BeanUtils.toBean(list, BaseXjRespVO.class));
    }

}